/*
 * Copyright (C) 2012 HTC Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.htc.sample.lockscreencompatibility;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import com.htc.lockscreen.fusion.idlescreen.SimpleIdleScreenEngine;
import com.htc.lockscreen.fusion.open.SimpleEngine;
import com.htc.sample.lockscreencompatibility.widget.AlarmAnime;

/**
 * The lock screen wrapper implementation class. <br>
 * Use this class to target legacy API and emulator support in addition to the current API. <br>
 * See AndroidManifest to toggle support for each of the three cases there.
 * 
 */
public class LockScreenImpl extends LockScreenImplBase implements LockScreenListener {

	//private AlarmAnime mClock;
	private class LogItem {
		public int category;
		public int position;
		public boolean liked;
		public int attention;
		public int relaxation;	
	}
	
	private int nextShotFrame = 1;
	private int shotFrames[] = {R.drawable.shot0, R.drawable.shot1, R.drawable.shot2, R.drawable.shot3};
    private boolean mStart;
    private int position = 0;
    private int category = 0;
    public int attention = 0;
    public int relaxation = 0;
    
    private TextView text_attention;
    private TextView text_relaxation;
    private ImageView image_content;
     
    public static int[][] text = {
    	{R.drawable.simpson1, R.drawable.simpson2, R.drawable.simpson3, R.drawable.simpsonad, R.drawable.simpson4, R.drawable.simpson5},
    	{R.drawable.starwars1, R.drawable.starwars2, R.drawable.starwars3, R.drawable.starwarsad, R.drawable.starwars4, R.drawable.starwars5},
    	{R.drawable.kitten1, R.drawable.kitten2, R.drawable.kitten3, R.drawable.kitten4, R.drawable.kitten5},
    	{R.drawable.puppy1, R.drawable.puppy2, R.drawable.puppyad, R.drawable.puppy3, R.drawable.puppy4}
    
    };
    public ArrayList<LogItem> itemLog;

	MWHeadsetController hsController;
	boolean useHeadset = true;
    public void refreshContent()
    {

		image_content.setImageResource(text[category][position]);
		
		
		if(useHeadset)
		{
			MWEvent att = HeadsetDataCapture.getLatestAttentionEvent();
			if(att != null)
			{
				attention = (int) att.getIntensity();
			}
			else
			{
				attention = 0;
			}
			
			MWEvent rel = HeadsetDataCapture.getLatestMeditationEvent();
			if(rel != null)
			{
				relaxation = (int) rel.getIntensity();
			}
			else
			{
				relaxation = 0;
			}
		}
		else
		{
			attention = relaxation = 0;
			
		}
		
		Log.d("lockscreen", "Attention:"+attention);
		Log.d("lockscreen", "Relaxation:"+relaxation);
		text_attention.setText("Attention: "+attention);
    	text_relaxation.setText("Relaxation: "+relaxation);
    }
    
    public void stats(int attention, int relaxation)
    {
    	this.attention = attention;
    	this.relaxation = relaxation;
    	Log.d("stats", "Attention:"+attention);
    	Log.d("stats", "Relaxation:"+relaxation);
    	
    }
    
    public void setLastAttention(int attention)
    {
    	this.attention = attention;
    }
    
    public void setLastRelaxation(int relaxation)
    {
    	this.relaxation = relaxation;
    }
    
    public boolean liked()
    {
		LogItem i = new LogItem();
		i.attention = attention;
		i.relaxation = relaxation;
		i.position = position;
		i.category = category;
		i.liked = true;
		
		itemLog.add(i);
    	
		position++;
		if(position >= text[category].length)
		{			
			category++;
			position = 0;
		}
		category = category % text.length;
		clearWeapon();
		refreshContent();
		return true;
    }

    private boolean disliked()
    {
		LogItem i = new LogItem();
		i.attention = attention;
		i.relaxation = relaxation;
		i.position = position;
		i.category = category;
		i.liked = false;
		
		itemLog.add(i);
    	
    	position++;
		if(position >= text[category].length)
		{
			category++;
			position = 0;
		}
		category = category % text.length;
		clearWeapon();
		refreshContent();
		return true;
	}

   public boolean nextCategory()
   {
		category++;
		category = category % text.length;
	
		position = 0;
		clearWeapon();
		refreshContent();
		return true;
   }
   
   public boolean toggleReport()
   {
	   return true;
   }
   	
    public LockScreenImpl(Context service, SimpleEngine engine) {
		super(service, engine);
		
		if(useHeadset)
			hsController  = new MWHeadsetController();
		
		itemLog = new ArrayList<LogItem>(); 
	}

	// support for older deprecated API
	public LockScreenImpl(Context service, SimpleIdleScreenEngine engine) {
		super(service, engine);

		if(useHeadset)
			hsController  = new MWHeadsetController();
		
		
		itemLog = new ArrayList<LogItem>(); 	
	}

	// support for running in an activity (emulator)
	public LockScreenImpl(Activity context) {
		super(context);
		
		if(useHeadset)
			hsController  = new MWHeadsetController();
		
		itemLog = new ArrayList<LogItem>(); 		
	}

	@Override
	public void onCreate(SurfaceHolder holder) {
		
		setContent(R.layout.main);
		setShowLiveWallpaper(true);

		if(useHeadset)
		{
			if(hsController.initiateHeadsetConnection(headsetHandler)){
				//doStuff2();
				hsController.openHeadsetDataPipeline();
			}
		}		
		
		text_relaxation = (TextView) findViewById(R.id.relaxation);
		text_attention = (TextView) findViewById(R.id.attention);
		image_content = (ImageView) findViewById(R.id.imageView1);
		refreshContent();
		ImageView like = (ImageView) findViewById(R.id.button_like);
	    like.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				liked();
			}
		});

	    ImageView dislike = (ImageView) findViewById(R.id.button_dislike);
	    dislike.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				disliked();
			}
		});
	    
	    ImageView nextCategory = (ImageView) findViewById(R.id.button_nextcategory);
	    nextCategory.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				nextCategory();
			}
		});

	    ImageView fireWeapon = (ImageView) findViewById(R.id.button_fireweapon);
	    fireWeapon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				fireWeapon();
			}
		});	    
	    
	    /*Button report = (Button) findViewById(R.id.button_report);
	    report.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				toggleReport();
			}
		});*/	    
	}
	
	private void fireWeapon() {
		ImageView image = (ImageView) findViewById(R.id.imageoverlay);
		image.setImageResource(shotFrames[nextShotFrame]);
		//nextShotFrame = (nextShotFrame+1)%shotFrames.length;
		nextShotFrame = Math.min(shotFrames.length-1, nextShotFrame+1);
		
		
	}

	private void clearWeapon() {
		ImageView image = (ImageView) findViewById(R.id.imageoverlay);
		image.setImageResource(shotFrames[0]);
		nextShotFrame = 1;
	}	
	
	@Override
	public void onStart() {
	}
	
	@Override
	public void onResume() {
	}
	
	@Override
	public void onPause() {
	}
	
	@Override
	public void onStop() {
	}
	
	@Override
	public void onDestroy() {
		if(useHeadset)
		{
			hsController.getTgDevice().close();
		}
	}
	
	// Headset
	private final Handler headsetHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	if(useHeadset) 
			{
				hsController.processMessage(msg);
				refreshContent();
			}
        }
    };


}
